package login;

import java.sql.*;

import java.util.*;

public class LoginService {
    public static void main(String[] args) {
        Map<String,String> map  = iniLogin();
        boolean flag = login(map);
        System.out.println(flag ? "登录成功" : "登陆失败");


    }

   /* private static int useDB() {

    }*/

    private static boolean login(Map<String, String> map) {
        boolean flag = false;
        String uname = map.get("uname");
        String psd = map.get("psd");
        //System.out.println(uname + psd);
        //获取到用户名和密码
        //链接数据库判断用户名和密码
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        //1注册驱动
        try {
            Class.forName("com.mysql.jdbc.Driver" );
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/hjy", "root","123456");
            ps = conn.prepareStatement("select * from t_user where uname = ? and psd = ? ");
            ps.setString(1, uname);
            ps.setString(2, psd);
            rs = ps.executeQuery();
            if (rs.next()) {
                flag = true;
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return flag;
    }

    private static Map<String, String> iniLogin() {
        Map<String,String> map = new HashMap<>();
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名");
        //用户名
        String uname = sc.nextLine();
        System.out.println("请输入密码");
        //密码
        String psd = sc.nextLine();
        map.put("uname",uname);
        map.put("psd",psd);
        return map;
    }
}
